package com.one.rope.mvp.web.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.one.rope.mvp.basecore.util.MyBeanUtils;
import com.one.rope.mvp.basecore.util.StringUtils;
import com.one.rope.mvp.web.business.dao.CompanyMemberMapper;
import com.one.rope.mvp.web.business.dao.CompanyProjectMapper;
import com.one.rope.mvp.web.business.dao.CompanyProjectPartnerMapper;
import com.one.rope.mvp.web.business.dao.MemberInfoMapper;
import com.one.rope.mvp.web.business.entity.Company;
import com.one.rope.mvp.web.business.dao.CompanyMapper;
import com.one.rope.mvp.web.business.entity.CompanyMember;
import com.one.rope.mvp.web.business.entity.CompanyProject;
import com.one.rope.mvp.web.business.entity.CompanyProjectPartner;
import com.one.rope.mvp.web.business.entity.MemberInfo;
import com.one.rope.mvp.web.business.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.rope.mvp.web.common.dto.admin.company.*;
import com.one.rope.mvp.web.common.dto.admin.partner.AdminCompanyPartnerListVO;
import com.one.rope.mvp.web.common.dto.admin.project.AdminCompanyProjectListVO;
import com.one.rope.mvp.web.common.dto.admin.team.AdminCompanyMemberListVO;
import com.one.rope.mvp.basecore.bean.AdminBusinessMessage;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.basecore.exception.BusinessDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业基本信息表 服务实现类
 * </p>
 *
 * @author wesley
 * @since 2020-02-20
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

  @Autowired
  CompanyMapper companyMapper;

  @Autowired
  CompanyProjectMapper companyProjectMapper;

  @Autowired
  CompanyProjectPartnerMapper companyProjectPartnerMapper;

  @Autowired
  CompanyMemberMapper companyMemberMapper;

  @Autowired
  MemberInfoMapper memberInfoMapper;

  @Override
  public ResponseObject companyList(AdminCompanyListDTO requestDTO) {
    AdminCompanyListVO companyListVO = new AdminCompanyListVO();
    QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
    if (!StringUtils.isNullOrEmpty(requestDTO.getName())) {
      queryWrapper.like("name", requestDTO.getName());
    }
    Page<Company> page = new Page<Company>(requestDTO.getPageNo(), requestDTO.getPageSize());
    IPage<Company> companyIPage = this.page(page, queryWrapper);
    List<Company> companyList = companyIPage.getRecords();
    List<AdminCompanyListItemVO> itemList = new ArrayList<>();
    companyList.forEach(item -> {
      AdminCompanyListItemVO itemVO = new AdminCompanyListItemVO();
      MyBeanUtils.copyPropertiesIgnoreNull(item, itemVO);
      itemList.add(itemVO);
    });
    setNumberProperties(itemList);
    companyListVO.setResults(itemList);
    companyListVO.initPageInfo(page);
    return new ResponseObject(companyListVO);
  }

  /**
   * 设置数量属性
   */
  private void setNumberProperties(List<AdminCompanyListItemVO> itemList) {
    if (itemList.size() == 0) {
      return;
    }
    List<Long> companyIdList = itemList.stream().map(AdminCompanyListItemVO::getId)
        .collect(Collectors.toList());
    List<CompanyProject> companyProjectList = companyProjectMapper
        .selectList(new QueryWrapper<CompanyProject>().in("company_id", companyIdList));
    List<CompanyProjectPartner> companyProjectPartnerList = companyProjectPartnerMapper.selectList(
        new QueryWrapper<CompanyProjectPartner>().in("company_id", companyIdList));
    Map<Long, List<CompanyProject>> projectGroupMap = companyProjectList.stream()
        .collect(Collectors.groupingBy(CompanyProject::getCompanyId));
    Map<Long, List<CompanyProjectPartner>> partnerGroupMap = companyProjectPartnerList.stream()
        .collect(
            Collectors.groupingBy(CompanyProjectPartner::getCompanyId));
    itemList.stream().forEach(item -> {
      List<CompanyProject> projectList = projectGroupMap.get(item.getId());
      if (null != projectList && projectList.size() > 0) {
        item.setProjectNumber(projectList.size());
      }

      List<CompanyProjectPartner> partnerList = partnerGroupMap.get(item.getId());
      if (null != partnerList && partnerList.size() > 0) {
        item.setPartnerNumber(partnerList.size());
      }

    });
  }

  @Override
  public ResponseObject companyDetail(Long id) {
    AdminCompanyDetailVO resultVO = new AdminCompanyDetailVO();
    Company company = getById(id);
    if (null == company) {
      throw new BusinessDataException(AdminBusinessMessage.COMPANY_NOT_EXIST);
    }
    MyBeanUtils.copyPropertiesIgnoreNull(company, resultVO);

    // 查询企业创建用户
    QueryWrapper<MemberInfo> memberQueryWrapper = new QueryWrapper<>();
    memberQueryWrapper.eq("company_id", id);
    memberQueryWrapper.eq("user_type", 2);
    memberQueryWrapper.eq("is_main", 1);
    MemberInfo memberInfo = memberInfoMapper.selectOne(memberQueryWrapper);
    if(null != memberInfo){
      CompanyCreateUserVO createUserVO = new CompanyCreateUserVO();
      MyBeanUtils.copyPropertiesIgnoreNull(memberInfo, createUserVO);
      resultVO.setCreateUser(createUserVO);
    }

    // 查询项目列表
    List<AdminCompanyProjectListVO> projectList = new ArrayList<>();
    List<CompanyProject> companyProjectList = companyProjectMapper.selectList(
        new QueryWrapper<CompanyProject>().eq("company_id", id)
    );
    if (companyProjectList.size() > 0) {
      companyProjectList.stream().forEach(item -> {
        AdminCompanyProjectListVO projectListVO = new AdminCompanyProjectListVO();
        MyBeanUtils.copyPropertiesIgnoreNull(item, projectListVO);
        projectList.add(projectListVO);
      });
    }

    // 查询合伙人列表
    List<AdminCompanyPartnerListVO> partnerList = new ArrayList<>();
    List<CompanyProjectPartner> companyProjectPartnerList = companyProjectPartnerMapper.selectList(
        new QueryWrapper<CompanyProjectPartner>().eq("company_id", id)
    );
    if (companyProjectPartnerList.size() > 0) {
      companyProjectPartnerList.stream().forEach(item -> {
        AdminCompanyPartnerListVO partnerListVO = new AdminCompanyPartnerListVO();
        MyBeanUtils.copyPropertiesIgnoreNull(item, partnerListVO);
        CompanyProject companyProject = companyProjectMapper.selectById(item.getProjectId());
        partnerListVO.setProjectName(companyProject.getName());
        partnerList.add(partnerListVO);
      });
    }

    // 查询团队成员列表
    List<AdminCompanyMemberListVO> memberList = new ArrayList<>();
    List<CompanyMember> companyMemberList = companyMemberMapper.selectList(
        new QueryWrapper<CompanyMember>().eq("company_id", id)
    );
    if (companyMemberList.size() > 0) {
      companyMemberList.stream().forEach(item -> {
        AdminCompanyMemberListVO memberListVO = new AdminCompanyMemberListVO();
        MyBeanUtils.copyPropertiesIgnoreNull(item, memberListVO);
        memberList.add(memberListVO);
      });
    }

    resultVO.setProjectList(projectList);
    resultVO.setPartnerList(partnerList);
    resultVO.setMemberList(memberList);
    return new ResponseObject(resultVO);
  }

  @Override
  public void editCompany(EditCompanyDTO requestDTO) {
    Company company = new Company();
    MyBeanUtils.copyPropertiesIgnoreNull(requestDTO, company);
    company.setIsSet(1);
    this.updateById(company);
  }
}
